package com.langshixiaobai.mqdemo.server.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.ReferenceType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

//交换机
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Exchange {
    //唯一标识
    private String name;
    //交换机类型
    private ExchangeType type;
    //是否持久化
    private Boolean durable = false;
    //是否自动删除
    private Boolean autoDelete = false;
    //arguments额外的参数选项
    private Map<String, Object> arguments = new HashMap<>();



    //针对数据库
    //获取参数
    public String getArguments() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            //转换为json字符串格式
            String jsonArguments = objectMapper.writeValueAsString(arguments);
            return jsonArguments;
        } catch (JsonProcessingException e) {
            System.out.println("转换json出现错误");
        }
        //返回空字符串
        return "{}";
    }
    //注入参数
    public void setArguments(String jsonArguments) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            //解析成HashMap<String, Object> 类型
            arguments = objectMapper.readValue(jsonArguments, new TypeReference<HashMap<String, Object>>() {
            });
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    //针对程序
    //获取参数
    public Object getArguments(String key){
        return arguments.get(key);
    }

    //设置参数
    public void setArguments(String key,Object value) {
        arguments.put(key,value);
    }

}
